/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.system.Capabilities;
	
	import multimedia.control.event.LoadImageEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.MediaElementVO;
	
	import org.osmf.elements.DurationElement;
	import org.osmf.elements.ImageElement;
	import org.osmf.elements.ParallelElement;
	import org.osmf.elements.TextElement;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaType;
	import org.osmf.media.URLResource;
	import org.osmf.traits.LoadTrait;
	import org.osmf.traits.MediaTraitType;

	public class LoadImageCommand extends SequenceCommand implements ICommand
	{
		private var e:LoadImageEvent;
		private var media:ParallelElement;
		private var image:MediaElement;
		private var source:MediaElement;
		
		private var height:Number;
		private var width:Number;

		
		override public function execute(event:CairngormEvent):void
		{
			this.e = event as LoadImageEvent;
			this.nextEvent = e.nextEvent;
			var model:ModelLocator = ModelLocator.getInstance();
			ModelLocator.getInstance().log += "\nLoadImageCommand";
			
			height = Capabilities.screenResolutionY
			width = Capabilities.screenResolutionX;
			
			var imageResource:URLResource = new URLResource(e.image.url);
			imageResource.mediaType = MediaType.IMAGE;//
			
			image = new DurationElement(e.image.duration,
						new ImageElement(imageResource));
			
			source = new DurationElement(e.image.duration,
						new TextElement("Quelle: " + e.image.image_source));
			
			media = new ParallelElement();
			
			media.addChild(image);
			//media.addChild(source);

			var imageLayout:LayoutMetadata = new LayoutMetadata();
			media.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, imageLayout);
			imageLayout.height = height;
			imageLayout.width = imageLayout.height * e.image.aspect_ratio;
			
			if(imageLayout.width > width)
			{
				imageLayout.width = width;
				imageLayout.height = imageLayout.width / e.image.aspect_ratio;
			}
			
			imageLayout.x = Capabilities.screenResolutionX * 0.5 - imageLayout.width * 0.5;
			imageLayout.y = Capabilities.screenResolutionY * 0.5 - imageLayout.height * 0.5;
			
			(media.getTrait(MediaTraitType.LOAD) as LoadTrait).load();
			
			model.mediaElements[(model.counter + 1)%4] = new MediaElementVO(e.image, media)
			
			executeNextCommand();			
		}
		
	}
}